/*     */ package com.zimbra.cs.session;
/*     */ 
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.service.mail.WaitSetRequest;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class WaitSetBase
/*     */   implements IWaitSet
/*     */ {
/*     */   protected final String mWaitSetId;
/*     */   protected final String mOwnerAccountId;
/*     */   protected final Set<MailItem.Type> defaultInterest;
/*     */   
/*     */   abstract Map<String, WaitSetAccount> destroy();
/*     */   
/*     */   abstract int countSessions();
/*     */   
/*     */   protected abstract boolean cbSeqIsCurrent();
/*     */   
/*     */   protected abstract String toNextSeqNo();
/*     */   
/*     */   public long getLastAccessedTime()
/*     */   {
/*  43 */     return this.mLastAccessedTime;
/*     */   }
/*     */   
/*     */   public void setLastAccessedTime(long lastAccessedTime) {
/*  47 */     this.mLastAccessedTime = lastAccessedTime;
/*     */   }
/*     */   
/*     */   public Set<MailItem.Type> getDefaultInterest()
/*     */   {
/*  52 */     return this.defaultInterest;
/*     */   }
/*     */   
/*     */   public String getOwnerAccountId()
/*     */   {
/*  57 */     return this.mOwnerAccountId;
/*     */   }
/*     */   
/*     */   public String getWaitSetId()
/*     */   {
/*  62 */     return this.mWaitSetId;
/*     */   }
/*     */   
/*  65 */   synchronized WaitSetCallback getCb() { return this.mCb; }
/*     */   
/*     */ 
/*     */ 
/*     */   protected synchronized void cancelExistingCB()
/*     */   {
/*  71 */     if (this.mCb != null)
/*     */     {
/*  73 */       this.mCb.dataReady(this, "", true, null, null);
/*  74 */       this.mCb = null;
/*  75 */       this.mLastAccessedTime = System.currentTimeMillis();
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void doneWaiting()
/*     */   {
/*  81 */     this.mCb = null;
/*  82 */     this.mLastAccessedTime = System.currentTimeMillis();
/*     */   }
/*     */   
/*     */   protected WaitSetBase(String ownerAccountId, String waitSetId, Set<MailItem.Type> defaultInterest)
/*     */   {
/*  87 */     this.mOwnerAccountId = ownerAccountId;
/*  88 */     this.mWaitSetId = waitSetId;
/*  89 */     this.defaultInterest = defaultInterest;
/*     */   }
/*     */   
/*     */   protected synchronized void trySendData() {
/*  93 */     boolean trace = ZimbraLog.session.isTraceEnabled();
/*  94 */     if (trace) { ZimbraLog.session.trace("WaitSetBase.trySendData 1");
/*     */     }
/*  96 */     if (this.mCb == null) {
/*  97 */       return;
/*     */     }
/*     */     
/* 100 */     if (trace) ZimbraLog.session.trace("WaitSetBase.trySendData 2");
/* 101 */     boolean cbIsCurrent = cbSeqIsCurrent();
/*     */     
/* 103 */     if (cbIsCurrent) {
/* 104 */       this.mSentSignalledSessions.clear();
/* 105 */       this.mSentErrors.clear();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 123 */     if ((this.mCurrentSignalledSessions.size() > 0) || (this.mCurrentErrors.size() > 0) || ((!cbIsCurrent) && ((this.mSentSignalledSessions.size() > 0) || (this.mSentErrors.size() > 0))))
/*     */     {
/*     */ 
/* 126 */       if (this.mSentSignalledSessions.size() == 0) {
/* 127 */         if (trace) { ZimbraLog.session.trace("WaitSetBase.trySendData 3a");
/*     */         }
/* 129 */         HashSet<String> temp = this.mCurrentSignalledSessions;
/* 130 */         this.mCurrentSignalledSessions = this.mSentSignalledSessions;
/* 131 */         this.mSentSignalledSessions = temp;
/*     */       } else {
/* 133 */         if (trace) ZimbraLog.session.trace("WaitSetBase.trySendData 3b");
/* 134 */         assert (!cbIsCurrent);
/* 135 */         this.mSentSignalledSessions.addAll(this.mCurrentSignalledSessions);
/* 136 */         this.mCurrentSignalledSessions.clear();
/*     */       }
/*     */       
/*     */ 
/* 140 */       this.mSentErrors.addAll(this.mCurrentErrors);
/* 141 */       this.mCurrentErrors.clear();
/*     */       
/*     */ 
/*     */ 
/* 145 */       assert ((this.mSentSignalledSessions.size() > 0) || (this.mSentErrors.size() > 0));
/* 146 */       String[] toRet = new String[this.mSentSignalledSessions.size()];
/* 147 */       int i = 0;
/* 148 */       for (String accountId : this.mSentSignalledSessions) {
/* 149 */         toRet[(i++)] = accountId;
/*     */       }
/*     */       
/* 152 */       if (trace) ZimbraLog.session.trace("WaitSetBase.trySendData 4");
/* 153 */       this.mCb.dataReady(this, toNextSeqNo(), false, this.mSentErrors, toRet);
/* 154 */       this.mCb = null;
/* 155 */       this.mLastAccessedTime = System.currentTimeMillis();
/*     */     }
/* 157 */     if (trace) ZimbraLog.session.trace("WaitSetBase.trySendData done");
/*     */   }
/*     */   
/*     */   public synchronized void handleQuery(Element response)
/*     */   {
/* 162 */     response.addAttribute("id", this.mWaitSetId);
/* 163 */     response.addAttribute("owner", this.mOwnerAccountId);
/* 164 */     response.addAttribute("defTypes", WaitSetRequest.expandInterestStr(this.defaultInterest));
/* 165 */     response.addAttribute("ld", this.mLastAccessedTime);
/*     */     Element errors;
/* 167 */     if (this.mCurrentErrors.size() > 0) {
/* 168 */       errors = response.addElement("errors");
/* 169 */       for (WaitSetError error : this.mCurrentErrors) {
/* 170 */         Element errorElt = errors.addElement("error");
/* 171 */         errorElt.addAttribute("id", error.accountId);
/* 172 */         errorElt.addAttribute("type", error.error.name());
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 177 */     if (this.mCurrentSignalledSessions.size() > 0) {
/* 178 */       Element signaled = response.addElement("ready");
/* 179 */       StringBuilder signaledStr = new StringBuilder();
/* 180 */       for (String accountId : this.mCurrentSignalledSessions) {
/* 181 */         if (signaledStr.length() > 0)
/* 182 */           signaledStr.append(",");
/* 183 */         signaledStr.append(accountId);
/*     */       }
/* 185 */       signaled.addAttribute("accounts", signaledStr.toString());
/*     */     }
/*     */   }
/*     */   
/*     */   protected synchronized void signalError(WaitSetError err) {
/* 190 */     this.mCurrentErrors.add(err);
/* 191 */     trySendData();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 199 */   protected long mLastAccessedTime = -1L;
/* 200 */   protected WaitSetCallback mCb = null;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/* 205 */   protected List<WaitSetError> mCurrentErrors = new ArrayList();
/* 206 */   protected List<WaitSetError> mSentErrors = new ArrayList();
/*     */   
/*     */ 
/*     */ 
/* 210 */   protected HashSet<String> mCurrentSignalledSessions = new HashSet();
/*     */   
/*     */ 
/* 213 */   protected HashSet<String> mSentSignalledSessions = new HashSet();
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/session/WaitSetBase.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */